const express = require('express');
const router = express.Router();
const PostService = require('../../service/post');
const _ = require('lodash');

const PostFields = ['_id','post_title','post_excerpt','post_content','post_term','post_author','post_slug','post_type'];

/**
 * 获取文章列表
 */
router.get('/list', function(req, res, next){
  const page = req.query.page || 1;
  let conditions = {};
  if(req.query.type && req.query.type === 'trash'){
    conditions = {post_status:-1}
  } else {
    conditions = {post_status:{$gt:-1}}
  }
  PostService.getList(page,conditions,{ post_content:0 }).then( (doc) => {
    return res.json({code: 0, msg: 'success', data: doc});
  }).catch(function (err) {
    return res.json({code: -1, msg: err, data: null})
  });
})

/**
 * 获取文章详情
 */
router.get('/detail', function(req, res, next){
  const _id = req.query._id;
  PostService.getDetail(_id).then( (doc) => {
    return res.json({code: 0, msg: 'success', data: doc});
  }).catch(function (err) {
    return res.json({code: -1, msg: err, data: null})
  });
})

/**
 * 新增文章
 */
router.post('/add', function(req, res, next){
  let options = _.pick(req.body,PostFields);
  options.post_author = req.session.user._id || 'admin';
  PostService.add(options).then( (doc) => {
    return res.json({code: 0, msg: 'success', data: doc});
  }).catch( (err) =>{
    return res.json({code: -1, msg: err, data: null})
  });
});

/**
 * 更新文章
 */
router.post('/update', function(req, res, next){
  const options = _.pick(req.body,PostFields);
  PostService.updateInfo(options).then((doc) => {
    return res.json({code: 0, msg: 'success', data: doc});
  }).catch((err)=>{
    return res.json({code: -1, msg: err, data: null})
  })
});


/**
 * 文章到回收站
 */
router.post('/trash', function(req, res, next){
  const options = {
    _id : req.body._id,
    post_status :  -1
  };
  PostService.updateInfo(options).then((doc)=>{
    return res.json({code: 0, msg: 'success', data: doc});
  }).catch((err)=>{
    return res.json({code: -1, msg: err, data: null})
  })
});

/**
 * 回收站到文章
 */
router.post('/recover', function(req, res, next){
  const options = {
    _id : req.body._id,
    post_status :  0
  };
  PostService.updateInfo(options).then((doc)=>{
    return res.json({code: 0, msg: 'success', data: doc});
  }).catch((err)=>{
    return res.json({code: -1, msg: err, data: null})
  })
});

/**
 * 删除文章
 */
router.post('/delete', function(req, res, next){
  PostService.remove(req.body._id).then((doc)=>{
    return res.json({code: 0, msg: 'success', data: doc});
  }).catch((err)=>{
    return res.json({code: -1, msg: err, data: null})
  })
});

var multer  = require('multer');
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads');    // 保存的路径，备注：需要自己创建
  },
  filename: function (req, file, cb) {
    var fileExtend = file.originalname.split('.').pop();
    cb(null, file.fieldname + '-' + Date.now() + '.' + fileExtend);
  }
});
var upload = multer({ storage: storage });

router.post('/upload',upload.single('file'), function (req,res,next) {
  console.log(req.file);

  return res.json({
    "success": true,
    "msg": "",
    "file_path": "/uploads/"+req.file.filename
  })
})

module.exports = router;
